/**
 * 
 */
package ken.app.xmlhandler;

import java.util.ArrayList;
import java.util.List;

import ken.app.bean.PersonalBean;
import ken.app.bean.TreeSingleBean;


import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

/**
 * @author ken.chung
 * 
 */
public class QueryPerSorXMLHandler extends DefaultHandler {
	
	/* 變數宣告 */
	private boolean in_content = false;
	private boolean in_membername = false;
	private boolean in_totalscore = false;	
	private boolean in_result = false;
	private boolean in_fruit_get = false;
	private boolean in_fruit_surplus = false;
	
	private List<TreeSingleBean> list;
//	private PersonalInfo personalInfo;
	private PersonalBean personalInfo;
	private StringBuffer buf = new StringBuffer();
	private String treename = "";

	/* 將轉換成List<News>的XML資料回傳 */
	public PersonalBean getParsedData() {
		return personalInfo;
	}

	/* XML文件開始解析時呼叫此method */
	@Override
	public void startDocument() throws SAXException {
		if (personalInfo == null) {
			personalInfo = PersonalBean.getInstance();
		}
	}

	/* XML文件結束解析時呼叫此method */
	@Override
	public void endDocument() throws SAXException {
	}

	/* 解析到Element的開頭時呼叫此method */
	@Override
	public void startElement(String namespaceURI, String localName,
			String qName, Attributes atts) throws SAXException {
		
			if (localName.equals("content")) {
				this.in_content = true;
				/* 解析到item的開頭時new一個News物件 */
				Log.d("ken", buf.toString());
			} else if (localName.equals("member_name")) {
				if (this.in_content) {
					this.in_membername = true;
				} 

			} else if (localName.equals("total_score")) {
				if (this.in_content) {
					this.in_totalscore = true;
				}
			} else if (localName.equals("fruit_get")) {
				if (this.in_content) {
					this.in_fruit_get = true;
				}
			} else if (localName.equals("fruit_surplus")) {
				if (this.in_content) {
					this.in_fruit_surplus = true;
				}
			} 

		buf.setLength(0);
	}

	/* 解析到Element的結尾時呼叫此method */
	@Override
	public void endElement(String namespaceURI, String localName, String qName)
			throws SAXException {
		
		if (localName.equals("content")) {
			this.in_content = false;
			/* 解析到item的結尾時將News物件寫入List中 */
//			personalInfo.setTreeName(treename);

		} else if (localName.equals("member_name")) {
			if (this.in_content) {
				/* 設定News物件的title */
				personalInfo.setMembername(buf.toString().trim());
				buf.setLength(0);
				this.in_membername = false;
			}
		} else if (localName.equals("total_score")) {
			if (this.in_content) {
				/* 設定News物件的link */
				personalInfo.setTotalScore(Integer.valueOf(buf.toString().trim()));
				buf.setLength(0);
				this.in_totalscore = false;
			}
		} else if (localName.equals("fruit_get")) {
			if (this.in_content) {
				/* 設定News物件的link */
				personalInfo.setFruitGet(Integer.valueOf(buf.toString().trim()));
				buf.setLength(0);
				this.in_fruit_get = false;
			}
		} else if (localName.equals("fruit_surplus")) {
			if (this.in_content) {
				/* 設定News物件的link */
				personalInfo.setFruitSurplus(Integer.valueOf(buf.toString().trim()));
				buf.setLength(0);
				this.in_fruit_surplus = false;
			}
		} 
	}

	/* 取得Element的開頭結尾中間夾的字串 */
	@Override
	public void characters(char ch[], int start, int length) {
		if (this.in_content || in_fruit_surplus) {
			/* 將char[]加入StringBuffer */
			buf.append(ch, start, length);
		}
	}
}